package com.beemans.common.utils.permission

import android.content.Context
import android.os.Build
import androidx.appcompat.app.AlertDialog
import com.hjq.permissions.Permission
import java.util.*

/**
 * @author tiamosu
 * @date 2021/7/14.
 */
object PermissionDialog {

    /**
     * 显示授权对话框
     */
    fun showPermissionDialog(
        context: Context,
        permissions: List<String>,
        onNegativeAction: () -> Unit = {},
        onPositiveAction: () -> Unit = {}
    ) {
        AlertDialog.Builder(context)
            .setCancelable(false)
            .setTitle("授权提醒")
            .setMessage(getPermissionHint(permissions))
            .setPositiveButton("前往授权") { dialog, _ ->
                dialog.dismiss()
                onPositiveAction.invoke()
            }
            .setNegativeButton("取消") { dialog, _ ->
                dialog.dismiss()
                onNegativeAction.invoke()
            }
            .show()
    }

    private fun getPermissionHint(permissions: List<String>): String {
        if (permissions.isEmpty()) {
            return "获取权限失败，请手动授予权限"
        }

        val hints: MutableList<String> = ArrayList()
        for (permission in permissions) {
            when (permission) {
                Permission.READ_EXTERNAL_STORAGE,
                Permission.WRITE_EXTERNAL_STORAGE,
                Permission.MANAGE_EXTERNAL_STORAGE -> {
                    val hint = "存储权限"
                    if (!hints.contains(hint)) {
                        hints.add(hint)
                    }
                }
                Permission.CAMERA -> {
                    val hint = "相机权限"
                    if (!hints.contains(hint)) {
                        hints.add(hint)
                    }
                }
                Permission.RECORD_AUDIO -> {
                    val hint = "麦克风权限"
                    if (!hints.contains(hint)) {
                        hints.add(hint)
                    }
                }
                Permission.ACCESS_FINE_LOCATION,
                Permission.ACCESS_COARSE_LOCATION,
                Permission.ACCESS_BACKGROUND_LOCATION -> {
                    val hint = if (!permissions.contains(Permission.ACCESS_FINE_LOCATION) &&
                        !permissions.contains(Permission.ACCESS_COARSE_LOCATION)
                    ) {
                        "后台定位权限"
                    } else {
                        "定位权限"
                    }
                    if (!hints.contains(hint)) {
                        hints.add(hint)
                    }
                }
                Permission.READ_PHONE_STATE,
                Permission.CALL_PHONE,
                Permission.ADD_VOICEMAIL,
                Permission.USE_SIP,
                Permission.READ_PHONE_NUMBERS,
                Permission.ANSWER_PHONE_CALLS -> {
                    val hint = "电话权限"
                    if (!hints.contains(hint)) {
                        hints.add(hint)
                    }
                }
                Permission.GET_ACCOUNTS,
                Permission.READ_CONTACTS,
                Permission.WRITE_CONTACTS -> {
                    val hint = "通讯录权限"
                    if (!hints.contains(hint)) {
                        hints.add(hint)
                    }
                }
                Permission.READ_CALENDAR,
                Permission.WRITE_CALENDAR -> {
                    val hint = "日历权限"
                    if (!hints.contains(hint)) {
                        hints.add(hint)
                    }
                }
                Permission.READ_CALL_LOG,
                Permission.WRITE_CALL_LOG,
                Permission.PROCESS_OUTGOING_CALLS -> {
                    val hint =
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) "通话记录权限" else "电话权限"
                    if (!hints.contains(hint)) {
                        hints.add(hint)
                    }
                }
                Permission.BODY_SENSORS -> {
                    val hint = "身体传感器权限"
                    if (!hints.contains(hint)) {
                        hints.add(hint)
                    }
                }
                Permission.ACTIVITY_RECOGNITION -> {
                    val hint = "健身运动权限"
                    if (!hints.contains(hint)) {
                        hints.add(hint)
                    }
                }
                Permission.SEND_SMS,
                Permission.RECEIVE_SMS,
                Permission.READ_SMS,
                Permission.RECEIVE_WAP_PUSH,
                Permission.RECEIVE_MMS -> {
                    val hint = "短信权限"
                    if (!hints.contains(hint)) {
                        hints.add(hint)
                    }
                }
                Permission.REQUEST_INSTALL_PACKAGES -> {
                    val hint = "安装应用权限"
                    if (!hints.contains(hint)) {
                        hints.add(hint)
                    }
                }
                Permission.NOTIFICATION_SERVICE -> {
                    val hint = "通知栏权限"
                    if (!hints.contains(hint)) {
                        hints.add(hint)
                    }
                }
                Permission.SYSTEM_ALERT_WINDOW -> {
                    val hint = "悬浮窗权限"
                    if (!hints.contains(hint)) {
                        hints.add(hint)
                    }
                }
                Permission.WRITE_SETTINGS -> {
                    val hint = "系统设置权限"
                    if (!hints.contains(hint)) {
                        hints.add(hint)
                    }
                }
            }
        }

        if (hints.isNotEmpty()) {
            val builder = StringBuilder()
            for (text in hints) {
                if (builder.isEmpty()) {
                    builder.append(text)
                } else {
                    builder.append("、")
                        .append(text)
                }
            }
            builder.append(" ")
            return "获取权限失败，请手动授予$builder"
        }
        return "使用此功能需要先授予权限"
    }
}